package com.chengccn.jpa.jpatest.service.impl;

import com.chengccn.jpa.jpatest.dao.SysUserRepository;
import com.chengccn.jpa.jpatest.entity.SysUser;
import com.chengccn.jpa.jpatest.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Administrator
 * @Description:
 * @Date: 2019/5/15 15:13
 * @Version: 1.0
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserRepository sysUserRepository;

    /**
     * 查询分页,无条件
     *
     * @param page 页数
     * @param size 显示条数
     * @return Page类
     */
    public Page<SysUser> findBookNoCriteria(Integer page, Integer size) {
        Pageable pageable = new PageRequest(page, size);
        return sysUserRepository.findAll(pageable);
    }

    /**
     * 查询分页 有条件
     *
     * @param page    页数
     * @param size    显示条数
     * @param sysUser 查询条件
     * @return Page类
     */
    public Page<SysUser> findBookCriteria(Integer page, Integer size, final SysUser sysUser) {
        Pageable pageable = new PageRequest(page, size);
        Page<SysUser> sysUsers = sysUserRepository.findAll(new Specification<SysUser>() {
            public Predicate toPredicate(Root<SysUser> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (StringUtils.isNotBlank(sysUser.getUsername())) {
                    list.add(criteriaBuilder.like(root.get("username").as(String.class), "%" + sysUser.getUsername() + "%"));
                }
                Predicate[] p = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(p));
            }
        }, pageable);
        return sysUsers;
    }

    /**
     * 条件查询 按名字模糊查询
     *
     * @param sysUser 条件
     * @return sysUser列表
     */
    public List<SysUser> getBySysUser(final SysUser sysUser) {
        List<SysUser> sysUsers = sysUserRepository.findAll(new Specification<SysUser>() {
            public Predicate toPredicate(Root<SysUser> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (StringUtils.isNotBlank(sysUser.getUsername())) {
                    list.add(criteriaBuilder.like(root.get("username").as(String.class), "%" + sysUser.getUsername() + "%"));
                }
                Predicate[] p = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(p));
            }
        });
        return sysUsers;
    }
}
